package com.dongdongshop.service.impl.item_jpa;

import com.dongdongshop.dao.item_jpa.ItemJpaDao;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.order_item.TbOrderItem;
import com.dongdongshop.service.item_jpa.ItemJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class ItemJpaServiceImpl implements ItemJpaService {
    @Autowired
    private ItemJpaDao itemJpaDao;
    @Override
    public List<TbItem> listItemByGoodsId(Long aLong) {
        return itemJpaDao.findAllByGoodsId(aLong);
    }

    @Override
    public TbItem getItemById(Long itemId) {
        return itemJpaDao.getItemByItemId(itemId);
    }

    @Override
    public void updateToNum(List<TbOrderItem> orderItemList) {
        for (TbOrderItem orderItem : orderItemList) {
            /*TbItem item = tbItemMapper.selectByPrimaryKey(orderItem.getItemId());*/
            TbItem item = itemJpaDao.getOne(orderItem.getItemId());
            Integer stockCount = item.getStockCount();
            Integer num = orderItem.getNum();
            int i = stockCount-num;
            item.setStockCount(i);
            /*tbItemMapper.updateByPrimaryKeySelective(item);*/
            itemJpaDao.save(item);
        }
    }
}
